using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Xml.Xsl;
using System.IO;

using System.Data;

using Pampa.Grid.Utils;

using Pampa.Grid.Core;
using Pampa.Grid.Data;
using Pampa.Grid.UI.Exceptions;
using Pampa.Grid.Exceptions;
using Pampa.Grid.Core.Filters;

using Pampa.WebControls;

namespace Pampa.Grid.UI.CustomControls
{

    [DesignerAttribute(typeof(BaseDesigner), typeof(System.ComponentModel.Design.IDesigner))]
    public abstract class AbstractPager : BaseControl
    {
        private string gridID;
        private bool show;

        public AbstractPager()
        {
            this.CssClass = "pagerDefault";
            show = true;
        }

        public string GridID
        {
            get { return gridID; }
            set { gridID = value; }
        }

        public bool Show
        {
            get { return show; }
            set { show = value; }
        }
        
        protected override void OnLoad(EventArgs e)
        {
            if (this.CssClass == "pagerDefault")
                RegisterStyleSheet("Pampa.Grid/Pager.css");

            RegisterLibrary("Pampa.Common/Common.js");
            RegisterLibrary("Pampa.Grid/Pager.js");

            base.OnLoad(e);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            CustomGrid parentList = CustomGrid.LoadFromSession(gridID);

            if(parentList!=null)
            {
                this.Show = parentList.ShowData;
                this.RenderContents(writer);

                writer.WriteLine("<script type=\"text/javascript\">");
                writer.WriteLine("var obj" + this.ID + "= new ClientPager(\"" + this.ID + "\",\"" + parentList.ID + "\");");
                RenderProperties(writer);
                writer.WriteLine("</script>");
            }
            else
            {
                writer.WriteLine("Pager Error. There is no CustomGrid called " + this.GridID);
            }
        }

        protected virtual void RenderProperties(HtmlTextWriter writer)
        {
            CustomGrid parentList = CustomGrid.LoadFromSession(gridID);

            writer.WriteLine("obj" + this.ID + ".pageNumber = " + parentList.PageNumber.ToString() + ";");
            writer.WriteLine("obj" + this.ID + ".lastPage = " + parentList.LastPage.ToString() + ";");
        }
    }

}
